package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.YddUser;
import com.itheima.ydd.service.IYddUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/ydd_user")
@Slf4j
public class YddUserController {
    @Autowired
    private IYddUserService yddUserService;

    @GetMapping("/list")
    public ResultData list(String sex, String username, String mobile, Integer page, Integer limit){
        log.info("sex = {},username = {},mobile = {},page = {},limit = {}",sex,username,mobile,page,limit);
        //构造分页构造器
        Page<YddUser> pageInfo = new Page(page,limit);

        //构造条件构造器
        LambdaQueryWrapper<YddUser> queryWrapper = new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(sex),YddUser::getSex,sex);
        queryWrapper.like(StringUtils.isNotEmpty(username),YddUser::getUsername,username);
        queryWrapper.like(StringUtils.isNotEmpty(mobile),YddUser::getMobile,mobile);
        //添加排序条件
        queryWrapper.orderByAsc(YddUser::getId);

        //执行查询
       yddUserService.page(pageInfo, queryWrapper);

        HashMap<String, Object> map = new HashMap<>();
        map.put("count",pageInfo.getTotal());
        map.put("totalPages",pageInfo.getPages());
        map.put("pageSize",pageInfo.getSize());
        map.put("currentPage",pageInfo.getCurrent());
        map.put("data",pageInfo.getRecords());
        return ResultData.ok("",map);
    }
    @PostMapping("/add")
    public R add(@RequestBody YddUser yddUser){

        String password = yddUser.getPassword();
        //1、将页面提交的密码password进行md5加密处理
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        yddUser.setPassword(password);
        yddUserService.save(yddUser);


        return R.success("","添加成功");

    }

    @GetMapping("rowInfo")
    public ResultData rowinfo(int  id){

        //YddUser yddUser = yddUserService.getById(id);

        LambdaQueryWrapper<YddUser> lqw = new LambdaQueryWrapper<>();
        lqw.eq(YddUser::getId, id);
        YddUser yddUser = yddUserService.getOne(lqw);

        yddUser.setPassword("");


        return ResultData.ok("",yddUser);

    }

    @PostMapping("/update")
    public R update(@RequestBody YddUser yddUser){

        log.info(yddUser.toString());

        yddUserService.updateById(yddUser);

        return R.success("","修改成功");

    }

    @GetMapping("del")
    public R delete(int id){
        log.info("删除会员用户的id:{}", id);

        yddUserService.removeById(id);

        return  R.success("","删除成功");

    }




}
